

##### Get village-level data on pradhan elections
vd35 = read.csv("data/vdeck35.csv")
vd36 = read_dta("data/vdeck36.dta")

#### Rename column names
colnames(vd35)
vd35 = vd35 %>%
  rename(period = q1, elec_year = q2, elec_month = q3, no_elected_panch_memb = q4,
         pop_panch = q5, vwreserv = q6, vscreserv = q7, vstreserv = q8, vobcreserv = q9)

vd36 = vd36 %>%
  rename(period = q1, cand_code = q2, cand_name = q3, recontester = q4, 
         gender = q5, educ = q6, religion = q7, caste = q8, 
         pol_exp = q9, elec_mode = q10, total_votes = q11, percent_votes = q12, 
         support_caste = q13, support_religion = q14, support_wealth = q15,
         out_support_caste = q16, out_support_religion = q17, out_support_party = q18)


##### Let's look at the gender composition of candidates by reservation type
election = vd36 %>% 
  mutate_at(vars(stateid, districtid, blockid, tehsilid, villageid), 
            .funs = function(x){as.numeric(x)}) %>%
  left_join(vd35) %>%
  arrange(stateid, districtid, blockid, tehsilid, villageid, period, cand_code)

# recode reservations
election = election %>%
  mutate(vwreserv = ifelse(vwreserv==2, 0, vwreserv)) %>%
  mutate(vstreserv = ifelse(vstreserv==2, 0, vstreserv)) %>%
  mutate(vscreserv = ifelse(vscreserv==2, 0, vscreserv)) %>%
  mutate(vobcreserv = ifelse(vobcreserv==2, 0, vobcreserv)) %>%
  mutate(period_new = case_when(
    period==1 ~ 3,
    period==2 ~ 2, 
    period==3 ~ 1
  )) %>%
  mutate(recontester = ifelse(recontester==2, 0, recontester)) %>%
  mutate(female = ifelse(gender==2, 1, 0)) %>%
  mutate(education_chr = case_when(
    educ==1 ~ "illiterate", 
    educ==2 ~ "primary school",
    educ==3 ~ "secondary school",
    educ==4 ~ "higher than secondary"
  )) %>%
  mutate(hindu = ifelse(religion==1, 1, 0)) %>%
  mutate(muslim = ifelse(religion==2, 1, 0)) %>%
  mutate(sikh = ifelse(religion==3, 1, 0)) %>%
  mutate(christian = ifelse(religion==4, 1, 0)) %>%
  mutate(jain = ifelse(religion==5, 1, 0)) %>%
  mutate(buddhist = ifelse(religion==6, 1, 0)) %>%
  mutate(relig_other = ifelse(religion==7, 1, 0)) %>%
  mutate(SC = ifelse(caste==1, 1, 0)) %>%
  mutate(ST = ifelse(caste==2, 1, 0)) %>%
  mutate(OBC = ifelse(caste==3, 1, 0)) %>% 
  mutate(OC = ifelse(caste==4, 1, 0)) %>%
  mutate(pol_exp = ifelse(pol_exp==2, 0, pol_exp)) %>%
  mutate(pol_exp = ifelse(pol_exp==3, NA_integer_, pol_exp)) %>%
  mutate(elec_mode = case_when(
    elec_mode==1 ~ "panch members",
    elec_mode==2 ~ "direct election",
    elec_mode==3 ~ "unopposed"
  ))

# Village support sources
election = election %>%
  mutate(support_caste = ifelse(support_caste>2, NA_integer_, support_caste)) %>%
  mutate(support_caste = ifelse(support_caste==1, 1, 0)) %>%
  mutate(support_religion = ifelse(support_religion>2, NA_integer_, support_religion)) %>%
  mutate(support_religion = ifelse(support_religion==1, 1, 0)) %>%
  mutate(support_wealth = ifelse(support_wealth==1, 1, 0)) 
# Village outside support
election = election %>%
  mutate(out_support_caste = ifelse(out_support_caste>2, NA_integer_, out_support_caste)) %>%
  mutate(out_support_caste = ifelse(out_support_caste==1, 1, 0)) %>%
  mutate(out_support_religion = ifelse(out_support_religion>2, NA_integer_, out_support_religion)) %>%
  mutate(out_support_religion = ifelse(out_support_religion==1, 1, 0)) %>%
  mutate(out_support_party = ifelse(out_support_party==1, 1, 0)) 



# let's look at the likelihood of women candidates without reservations
election = election %>%
  mutate(reservation_type = case_when(
    vwreserv==0 & vstreserv==0 & vscreserv==0 ~ "No reservations", 
    vwreserv==0 & vstreserv==1 & vscreserv==0 ~ "ST reservations",
    vwreserv==0 & vstreserv==0 & vscreserv==1 ~ "SC reservations",
    vwreserv==1 & vstreserv==0 & vscreserv==0 ~ "Women reservations",
    vwreserv==1 & (vstreserv==1 & vscreserv==0) ~ "Women X ST res.",
    vwreserv==1 & (vstreserv==0 & vscreserv==1) ~ "Women X SC res."
  ))

library(ggpubr)
# sources of support by reservation type
election %>%
  group_by(reservation_type) %>%
  summarise_at(vars(support_caste, support_religion, support_wealth),
               .funs = mean,  na.rm = T) %>%
  filter(!is.na(reservation_type)) %>%
  melt(id.vars = "reservation_type") %>%
  mutate(pos = value/2) %>%
  mutate(label = round(value, 2)) %>%
  mutate(var_label = case_when(
    variable=="support_caste" ~ "Caste",
    variable=="support_religion" ~ "Religion",
    variable=="support_wealth" ~ "Wealth"
  )) %>%
  ggplot(., aes(x = reservation_type, y = value))+ 
  geom_bar(stat = "identity")+
  geom_text(aes(y = pos, label = label), size = 4, color = "white")+
  labs(x= "Quota Type", y = "% of Candidates", 
       subtitle = "Voters' support for candidates is based on...")+
  theme_pubr()+
  theme(axis.text = element_text(size = 12, angle = 45, hjust = 1),
        axis.title = element_text(face = "bold", size = 17),
        legend.title = element_text(face = "bold", size = 16),
        legend.text = element_text(face = "bold", size = 15))+
  facet_grid(.~var_label)
ggsave(filename = paste0(fig.out, "FigureH10.pdf"))



# outside sources of support by reservation type
election %>%
  group_by(reservation_type) %>%
  summarise_at(vars(out_support_caste, out_support_religion, out_support_party),
               .funs = mean,  na.rm = T) %>%
  filter(!is.na(reservation_type)) %>%
  melt(id.vars = "reservation_type") %>%
  mutate(pos = value/2) %>%
  mutate(label = round(value, 2)) %>%
  mutate(var_label = case_when(
    variable=="out_support_caste" ~ "Caste",
    variable=="out_support_religion" ~ "Religion",
    variable=="out_support_party" ~ "Political Party"
  )) %>%
  ggplot(., aes(x = reservation_type, y = value))+ 
  geom_bar(stat = "identity")+
  geom_text(aes(y = pos, label = label), size = 4, color = "white")+
  labs(x= "Quota Type", y = "% of Candidates", 
       subtitle = "Outside support for candidates is based on...")+
  theme_pubr()+
  theme(axis.text = element_text(size = 12, angle = 45, hjust = 1),
        axis.title = element_text(face = "bold", size = 17),
        legend.title = element_text(face = "bold", size = 16),
        legend.text = element_text(face = "bold", size = 15))+
  facet_grid(.~var_label)
ggsave(filename = paste0(fig.out, "FigureH11.pdf"))








